package com.hsg.common.thirdloginutil.login

import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.hsg.common.thirdloginutil.bean.ThirdLoginRespond

/**
 * 基础登入工具类
 */
open class BaseOtherLogin(
    /**
     * Activity 和 Fragment 二选一
     */
    protected val activity: AppCompatActivity? = null,
    protected val fragment: Fragment? = null,
    /**
     * 登入回调，登入成功有ThirdLoginRespond，否则ThirdLoginRespond为null
     */
    protected val loginCallback: (respond: ThirdLoginRespond?) -> Unit
) {
    protected lateinit var context: Context
    protected val TAG = this.javaClass.simpleName

    /**
     * 通用的Activity对象，例如出入Fragment时，供某些方法要使用Activity作为参数时使用。
     */
    protected lateinit var commonActivity: Activity

    init {
        if (activity == null && fragment == null) {
            Log.e(TAG, "Both activity and fragment is null.")
            loginCallback(null)
        } else {
            context = activity ?: fragment!!.requireContext()
            commonActivity = activity ?: fragment!!.requireActivity()
        }
    }

}